/**
 * @author Tobias Boehm
 *
 * 23.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

import org.anddev.andengine.extension.tbsengine.match.AbsServerMatch;
import org.anddev.andengine.extension.tbsengine.match.AbsServerMatchPreparation;

/**
 * This is an abstract class for server messages which are related to an
 * instance of an implementation of {@link AbsServerMatchPreparation} or
 * {@link AbsServerMatch}.
 * <p>
 * You should extend from this class if you want to develop new match related
 * messages. 
 * 
 * @author Tobias Boehm
 *
 */
public abstract class AbsServerMatchMessage extends SecureServerMessage implements DefaultTBSserverMessageFlags {

	protected UUID mMatchID = null;

	protected abstract void onReadTransmissonMatchData(final DataInputStream pDataInputStream) throws IOException;
	
	protected abstract void onWriteTransmissonMatchData(final DataOutputStream pDataOutputStream) throws IOException;
	
	
	@Override
	protected void onReadDecryptedTransmissionData(
			DataInputStream pDataInputStream) throws IOException  {
		
		this.mMatchID = UUID.fromString(pDataInputStream.readUTF());

		this.onReadTransmissonMatchData(pDataInputStream);
	}
	
	
	@Override
	protected void onWriteEncryptedTransmissionData(
			DataOutputStream pDataOutputStream) throws IOException {

		pDataOutputStream.writeUTF(this.mMatchID.toString());
		
		this.onWriteTransmissonMatchData(pDataOutputStream);
	}
	
	
	/**
	 * @return the mMatchID
	 */
	public UUID getMatchID() {
		return mMatchID;
	}

	/**
	 * @param mMatchID the mMatchID to set
	 */
	public void setMatchPreparation(final AbsServerMatchPreparation pMatch) {
		this.mMatchID = pMatch.getMatchID();
	}
	
	public void setMatch(final AbsServerMatch pServerMatch){
		
		this.mMatchID = pServerMatch.getMatchID();
	}
}
